﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SimpleJson;
using Pomelo.DotNetClient;


namespace SimpleClient
{
    public partial class Main : Form
    {

        private string _gate_server_ip = "127.0.0.1";
        private int _gate_server_port = 3014;

        public static JsonObject _users = null;
        public static PomeloClient _pomelo = null;


        public static int xMin;
        public static int yMin;
        public static int xMax;
        public static int yMax;
        public Floor f;
        public Main()
        {
            InitializeComponent();
            xMin = 40;
            yMin = 40;
            this.KeyPreview = true;
            this.DoubleBuffered = true;
            Display();

        }
        public void Display()
        {
            f = new Floor(new Point(xMin, yMin));
            Bitmap bmp = new Bitmap(Width, Height);
            BackgroundImage = bmp;
            Graphics g = Graphics.FromImage(bmp);
            f.Init(g);
        }
        public void UpDate()
        {
            Bitmap bmp = new Bitmap(Width, Height);
            BackgroundImage = bmp;
            Graphics g = Graphics.FromImage(bmp);
            f.UpDate(g);
        }
        
        private void Main_KeyDown(object sender, KeyEventArgs e)
        {
            if (!f.alive)
                return;
            int k, d = -1;
            k = e.KeyValue;
            switch(e.KeyCode)
            {
                case Keys.Up:
                    d = 1;
                    break;
                case Keys.Down:
                    d = 0;
                    break;
                case Keys.Right:
                    d = 2;
                    break;
                case Keys.Left:
                    d = 3;
                    break;
                case Keys.Space:
                    Attack();
                    break;
                default:
                    break;
            }
           
            Bitmap bmp = new Bitmap(Width, Height);
            BackgroundImage = bmp;
            Graphics g = Graphics.FromImage(bmp);
            f.Move(g, d);
        }
        private void Main_KeyUp(object sender, KeyEventArgs e)
        {

        }
        private void Attack()
        {
            f.P.attck();
        }
        private void mainTimerTick(object sender, EventArgs e)
        {
            UpDate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoginGateServer(uidEdit.Text);
        }
        void LoginGateServer(string userName)
        {
            _pomelo = new PomeloClient(_gate_server_ip, _gate_server_port);
            Console.WriteLine("开始连接 gate server  " + _gate_server_ip + ":" + _gate_server_port);
            _pomelo.connect(null, (data) =>
            {
                Console.WriteLine("成功连接 gate server ");
                JsonObject msg = new JsonObject();
                msg["uid"] = userName;

                _pomelo.request("gate.gateHandler.queryEntry", msg, LoginGateServerCallback);
            });
        }
        void LoginGateServerCallback(JsonObject result)
        {
            if (Convert.ToInt32(result["code"]) == 200)
            {
                _pomelo.disconnect();
                LoginConnectorServer(result);
            }
            else
            {
                Console.WriteLine("oh shit... Cannot access connector..");
            }
        }
        void LoginConnectorServer(JsonObject result)
        {

            string host = (string)result["host"];
            int port = Convert.ToInt32(result["port"]);

            Console.WriteLine("Connector Server 分配成功 " + host + ":" + port);

            _pomelo = new PomeloClient(host, port);

            _pomelo.connect(null, (data) =>
            {
                Console.WriteLine("成功连接 connector server " + host + ":" + port);
                JoinChannel(uidEdit.Text, channelEdit.Text);
            });
        }
        void JoinChannel(string userName, string channel)
        {
            JsonObject userMessage = new JsonObject();
            userMessage.Add("username", userName);
            userMessage.Add("rid", channel);

            if (_pomelo != null)
            {
                //请求加入聊天室
                _pomelo.request("connector.entryHandler.enter", userMessage, (data) =>
                {
                    _users = data;
                    if (_users != null)
                    {
                        Console.WriteLine("进入 channel:" + _users.ToString());
                    }
                    else
                    {
                        Console.WriteLine("进入 channel,but users == null");
                    }

                    InitGame();
                });
            }
        }
        void InitGame()
        {
            
            System.Object users = null;

            if (_users.TryGetValue("users", out users))
            {
                string u = users.ToString();
                string[] initUsers = u.Substring(1, u.Length - 2).Split(new Char[] { ',' });
                int length = initUsers.Length;
                for (int i = 0; i < length; i++)
                {
                    string s = initUsers[i];                    
                }
            }


            _pomelo.on("onAdd", (data) =>
            {
                Console.WriteLine("onAdd:" + data);
            });

            _pomelo.on("onLeave", (data) =>
            {
                Console.WriteLine("onLeave:" + data);
            });

            _pomelo.on("onGame", (data) =>
            {
                MessageBox.Show("成功");
            });

            _pomelo.on("disconnect", delegate(JsonObject msg)
            {
                Console.WriteLine("disconnect : " + msg);
            });
            MessageBox.Show("链接成功");
            f.connect();
        }
    }
}
